package com.wei.controller;


import com.wei.entity.GdAccount;
import com.wei.mapper.GdAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdAccountController {

    @Autowired
    private GdAccountMapper accountMapper;

    @RequestMapping("/getAccounts")
    public List<GdAccount> getAccount(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        return accountMapper.selectByMap(map);
    }

    @RequestMapping("/addAccount")
    public void setAccount(@RequestParam Map<String,Object> accountObj){
        GdAccount account =setParam(accountObj);
        accountMapper.insert(account);
    }

    @RequestMapping("/updateAccount")
    public void updateAccount(@RequestParam Map<String,Object> accountObj){
        GdAccount account = setParam(accountObj);
        accountMapper.updateById(account);
    }

    @RequestMapping("/deleteAccount")
    public void deleteAccount(@RequestParam String accountId){
        accountMapper.deleteById(accountId);
    }


    @RequestMapping("/getAccount")
    public String getAccount(@RequestParam Map<String,Object> accountObj){
        String pwd;
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("account_username", accountObj.get("accountUsername"));
        List<GdAccount> accounts=accountMapper.selectByMap(map);
        if(accounts.size()==0){
            return "nothing";
        }else{
            if(accounts.get(0).getAccountPassword().equals(accountObj.get("accountPassword"))){
                return accounts.get(0).getAccountType()+"---"+accounts.get(0).getAccountId()+"---"+accounts.get(0).getAccountType();
            }else{
                return "pwdErro";
            }
        }

    }


    /**
     * 通过账号id，用户名查询是否存在该账号，用于在路由过滤中验证session的user数据是否正确
     * @param params
     * @return
     */
    @RequestMapping("/hasAccount")
    public Boolean hasAccount(@RequestParam Map<String,Object> params){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("account_id", params.get("accountId"));
        map.put("account_username", params.get("accountUsername"));
        List<GdAccount> accounts=accountMapper.selectByMap(map);
        if(accounts.size()==0){
            return false;
        }else{
            return true;
        }

    }

    /**
     * 设置参数方法
     * @param accountObj
     * @return
     */
    private GdAccount setParam(@RequestParam Map<String, Object> accountObj) {
        GdAccount account=new GdAccount();
        account.setAccountId((String) accountObj.get("accountId"));
        account.setAccountForeignkey((String) accountObj.get("accountForeignkey"));
        account.setAccountUsername((String) accountObj.get("accountUsername"));
        account.setAccountPassword((String) accountObj.get("accountPassword"));
        account.setAccountType((String) accountObj.get("accountType"));
        account.setPropertyAddress((String) accountObj.get("propertyAddress"));
        return account;
    }

}

